package com.jjw.springbootcvs.controller;

import com.github.pagehelper.PageInfo;

import com.jjw.springbootcvs.config.BootCvsProperties;
import com.jjw.springbootcvs.pojo.dto.StorageRecordAndSupplierDTO;
import com.jjw.springbootcvs.pojo.entity.StorageRecord;
import com.jjw.springbootcvs.pojo.entity.Supplier;
import com.jjw.springbootcvs.pojo.entity.SysUser;
import com.jjw.springbootcvs.pojo.vo.PageStorageRecordListVO;
import com.jjw.springbootcvs.service.StorageRecordService;
import com.jjw.springbootcvs.service.SupplierService;
import com.jjw.springbootcvs.utils.Result;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaojuan
 * @Date 2024/11/2
 * @Description
 */
@Controller
@RequestMapping("/sys/storageRecord/")
@RequiredArgsConstructor
@Slf4j
public class StorageRecordConstant {


    private final StorageRecordService storageRecordService;

    private final SupplierService supplierService;

    private final BootCvsProperties bootCvsProperties;


    /**
     * 跳转库存列表
     * @param storageRecordAndSupplierDTO
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(StorageRecordAndSupplierDTO storageRecordAndSupplierDTO, Model model){
        List<Supplier> supplier = supplierService.getSupplierList();
        PageInfo<PageStorageRecordListVO> storageRecordList = storageRecordService.getAllStorageRecordList(storageRecordAndSupplierDTO);
        model.addAttribute("supplierList",supplier);
        model.addAttribute("storageRecordList",storageRecordList.getList());
        model.addAttribute("totalCount",storageRecordList.getTotal());
        model.addAttribute("currentPageNo",storageRecordList.getPageNum());
        model.addAttribute("totalPageCount",storageRecordList.getPages());
        return "storageRecord/list";
    }

    /**
     * 添加库存
     * @param storageRecord
     * @param session
     * @return
     */
    @RequestMapping("/add")
    public String add(StorageRecord storageRecord, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        storageRecord.setCreatedUserId(user.getId());
        storageRecord.setUpdatedUserId(user.getId());
        storageRecordService.add(storageRecord);
        return "redirect:/sys/storageRecord/list";
    }

    /**
     * 修改库存
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public String update(@PathVariable("id") Long id,Model model){
        StorageRecord storageRecord = storageRecordService.getStorageRecordById(id);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/update";
    }

    /**
     * 修改库存
     * @param storageRecord
     * @param session
     * @return
     */
    @PostMapping("/update")
    public String update(StorageRecord storageRecord, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        storageRecord.setUpdatedUserId(user.getId());
        storageRecordService.update(storageRecord);
        return "redirect:/sys/storageRecord/list";
    }

    /**
     * 删除库存
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    @ResponseBody
    private Result delete(@PathVariable("id") Long id){
        storageRecordService.delete(id);
        return Result.success();
    }

    /**
     * 查看详细信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        StorageRecord storageRecord = storageRecordService.getStorageRecordById(id);
        model.addAttribute("storageRecord",storageRecord);
        return "storageRecord/view";
    }






}
